<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgl - GLTFloader</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<link type="text/css" rel="stylesheet" href="main.css">
	</head>

	<body>
		<div id="info">
			<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - GLTFLoader<br />
			<a href="https://hdrihaven.com/hdri/?h=royal_esplanade" target="_blank" rel="noopener">Royal Esplanade</a> from <a href="https://hdrihaven.com/" target="_blank" rel="noopener">HDRI Haven</a>
		</div>

		<script type="importmap">
			{
				"imports": {
					"three": "../build/three.module.js",
					"three/addons/": "./jsm/"
				}
			}
		</script>

		<script type="module">

			import * as THREE from 'three';

			import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
			import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
			import { UltraHDRLoader } from 'three/addons/loaders/UltraHDRLoader.js';
			import { GUI } from 'three/addons/libs/lil-gui.module.min.js';

			let camera, scene, renderer, controls;
			let currentModel, mixer;
			let currentLoadId = 0;

			const timer = new THREE.Timer();

			init();

			function init() {

				const container = document.createElement( 'div' );
				document.body.appendChild( container );

				camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 20 );
				camera.position.set( - 1.8, 0.6, 2.7 );

				scene = new THREE.Scene();

				new UltraHDRLoader()
					.setPath( 'textures/equirectangular/' )
					.load( 'royal_esplanade_2k.hdr.jpg', function ( texture ) {

						texture.mapping = THREE.EquirectangularReflectionMapping;

						scene.background = texture;
						scene.environment = texture;

						render();

						// model

						fetch( 'https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Assets/main/Models/model-index.json' )
							.then( response => response.json() )
							.then( models => {

								const gui = new GUI();
								const modelNames = models.map( m => m.name );
								const params = { model: 'DamagedHelmet' };

								if ( ! modelNames.includes( params.model ) && modelNames.length > 0 ) {

									params.model = modelNames[ 0 ];

								}

								gui.add( params, 'model', modelNames ).onChange( name => {

									const modelInfo = models.find( m => m.name === name );
									loadModel( modelInfo );

								} );

								gui.add( scene, 'backgroundBlurriness', 0, 1 );

								const initialModel = models.find( m => m.name === params.model );
								if ( initialModel ) loadModel( initialModel );

							} );

					} );

				renderer = new THREE.WebGLRenderer( { antialias: true } );
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				renderer.setAnimationLoop( render );
				renderer.toneMapping = THREE.ACESFilmicToneMapping;
				container.appendChild( renderer.domElement );

				controls = new OrbitControls( camera, renderer.domElement );
				controls.enableDamping = true;
				controls.minDistance = 2;
				controls.maxDistance = 10;
				controls.target.set( 0, 0, - 0.2 );
				controls.update();

				window.addEventListener( 'resize', onWindowResize );

			}

			function loadModel( modelInfo ) {

				const variants = modelInfo.variants;
				const variant = variants[ 'glTF-Binary' ] || variants[ 'glTF' ];
				const url = `https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Assets/main/Models/${ modelInfo.name }/${ variant.endsWith( '.glb' ) ? 'glTF-Binary' : 'glTF' }/${ variant }`;

				if ( currentModel ) {

					scene.remove( currentModel );
					currentModel = null;

				}

				if ( mixer ) {

					mixer.stopAllAction();
					mixer = null;

				}

				const loadId = ++ currentLoadId;

				const loader = new GLTFLoader();
				loader.load( url, async function ( gltf ) {

					if ( loadId !== currentLoadId ) return;

					currentModel = gltf.scene;

					// wait until the model can be added to the scene without blocking due to shader compilation

					await renderer.compileAsync( currentModel, camera, scene );

					if ( loadId !== currentLoadId ) return;

					scene.add( currentModel );

					fitCameraToSelection( camera, controls, currentModel );

					// animations

					if ( gltf.animations.length > 0 ) {

						mixer = new THREE.AnimationMixer( currentModel );

						for ( const animation of gltf.animations ) {

							mixer.clipAction( animation ).play();

						}

					}

				} );

			}

			function fitCameraToSelection( camera, controls, selection, fitOffset = 1.3 ) {

				const box = new THREE.Box3();
				box.setFromObject( selection );

				const size = box.getSize( new THREE.Vector3() );
				const center = box.getCenter( new THREE.Vector3() );

				const maxSize = Math.max( size.x, size.y, size.z );
				const fitHeightDistance = maxSize / ( 2 * Math.atan( Math.PI * camera.fov / 360 ) );
				// const fitWidthDistance = fitHeightDistance / camera.aspect;
				// const distance = fitOffset * Math.max( fitHeightDistance, fitWidthDistance );
				const distance = fitOffset * fitHeightDistance;

				const direction = controls.target.clone().sub( camera.position ).normalize().multiplyScalar( distance );

				controls.maxDistance = distance * 10;
				controls.minDistance = distance / 10;
				controls.target.copy( center );

				camera.near = distance / 100;
				camera.far = distance * 100;
				camera.updateProjectionMatrix();

				camera.position.copy( controls.target ).sub( direction );

				controls.update();

			}

			function onWindowResize() {

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );

				render();

			}

			//

			function render() {

				timer.update();

				controls.update();

				if ( mixer ) mixer.update( timer.getDelta() );

				renderer.render( scene, camera );

			}

		</script>

	</body>
</html>
